package controller;

import jakarta.annotation.Resource;
import lombok.val;
import org.springframework.ai.image.ImagePrompt;
import org.springframework.ai.image.ImageResponse;
import org.springframework.ai.openai.OpenAiChatOptions;
import org.springframework.ai.openai.OpenAiImageModel;
import org.springframework.ai.openai.OpenAiImageOptions;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ImageController {

    @Resource
    private OpenAiImageModel openAiImageModel;

    /**
     * 输入字符串信息来生成图片
     * imageResponse.getResult().getOutput() 只是生成了图片的url，我们再点击链接对图片查看
     * @param msg
     * @return
     */
    @RequestMapping("/ai/image")
    public Object image(@RequestParam("msg") String msg){
        ImageResponse imageResponse = openAiImageModel.call(new ImagePrompt(msg));
        //获得到图片的url，完后可以处理图片
        String  url = imageResponse.getResult().getOutput().getUrl();

        return imageResponse.getResult().getOutput();
    }

    @RequestMapping("/ai/image2")
    public Object image2(@RequestParam("msg") String msg){
        ImageResponse imageResponse = openAiImageModel.call(new ImagePrompt(msg,
                OpenAiImageOptions.builder()
                .withQuality("hd")  //质量(高清)
                .withN(4)  //生成几张图片，根据模型决定，1~10之间
                .withHeight(1024)  //尺寸
                .withWidth(1024).build())
               );
        //获得到图片的url，完后可以处理图片
        String  url = imageResponse.getResult().getOutput().getUrl();

        return imageResponse.getResult().getOutput();
    }

}
